﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Ablactationss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Saless
{
    public class AddSalesHander : IRequestHandler<AddSalessCommand, Result>
    {

        private readonly IMapper _mapper;
        private readonly IBaseRepository<Sales> salesRepository;

        public AddSalesHander(IMapper mapper, IBaseRepository<Sales> salesRepository)
        {
            _mapper = mapper;
            this.salesRepository = salesRepository;
        }
        /// <summary>
        /// 销售添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddSalessCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            var res = salesRepository.AddRange(request.Sales);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "猪只销售添加成功";
            }

            return result;
        }
    }
}
